<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Messenger/classes/PhpFox_ComponentCompose.class.php, $Author:  $ */



/** Compose component
 *
 * @package module.messenger
 * @version $Id: PhpFox_ComponentCompose.class.php 465 2008-06-05 16:02:34Z phpfoxsv $
 *   PHPFox Team ()
 */
class PhpFox_Mod_Messenger_ComponentCompose extends BaseComponent
{
    function process()
    {
        App::loadClass('Text');
        $oTxt = &new Text();

        $sBoxName = App::format('compose');
        $oSrvMessenger = $this->_oModule->getService('Messenger');
        $oSrvAcc = &App::getModuleService('Account', 'Security');
        $oSrvBlock = &App::getModuleService('Account', 'Block');
        $oCurrUser = &$oSrvAcc->getCurrentUser();
        $oSrvFriend = &App::getModuleService('Account', 'Friends');

        $aMess = array();
        $oReq = &App::getRequest();
        $bSingle = ($oReq->getInt('bSingle')) ? true : false;
        if($oReq->get('user'))
        {
        	$bSingle = true;
        	$sRecipient = $oReq->get('user');
        }
        // validator
        $aFields = array(
			'aMess[title]'   => array('title'=>App::format('Mod_Messenger.subject'), 'def'=>'required'),
        );
        if(!$bSingle)
        	$aFields['aMess[to]'] = array('title'=>App::format('Mod_Messenger.to'), 'def'=>'required');
       	$oValidator = &App::newObject('Validator', $aFields);
        $aPost = $oReq->getArray('aMess');
        
        if (!isset($aPost['to']))
        {
            $aPost['to'] = '';
        }
        $sRecipient = !empty($_SESSION['compose']['user']) ? $_SESSION['compose']['user'] : (($oReq->get('user')) ? $oReq->get('user') : $aPost['to']);
		
        if($oReq->getInt('rep'))
        {
            $aMalInfo = $oSrvMessenger->getMessage($oReq->getInt('rep'));
            $sRecipient = $aMalInfo['from'];
            // $aMess['text'] = chr(10).chr(10).chr(10).chr(10).'[quote="'.$aMalInfo['from'].'"]'.chr(10).$aMalInfo['text'].chr(10).'[/quote]';
        }
        
        if($oReq->getInt('quot'))
        {
            $bQuot = true;
            $aMalInfo = $oSrvMessenger->getMessage($oReq->getInt('quot'));
            if ($aMalInfo['to'] == App::getUser('user'))
            {
            	$sRecipient = $aMalInfo['from'];
            	$aMess['text'] = chr(10).chr(10).chr(10).chr(10).'[quote="'.$aMalInfo['from'].'"]'.chr(10).$aMalInfo['text'].chr(10).'[/quote]';
            }
        }

        switch ($oReq->getAction())
        {
            case 'sSend':
                $aMemberShip = $oSrvAcc->getUserMembershipData($oSrvAcc->getUserByName($sRecipient));

                if(!$oSrvMessenger->checkInboxLimit($sRecipient, $aMemberShip, 'sender'))
                {
                    $this->addErrors($oSrvMessenger->getErrors());
				}
                if(strtolower($oCurrUser->aData['user']) == strtolower($aPost['to']))
                {
                    $this->addErrors(App::format('Mod_messenger.to.owner'));
				}
                if(!$oValidator->isValid($oReq->getAll()))
                {
                	$this->addErrors($oValidator->getErrors());
				}

				if( !App::getUserDetails($sRecipient, 'user') )
				{
                    $this->addErrors(App::format('Mod_messenger.user.nouser'));
				}
				else
				{
                	if($oSrvBlock->isBlocked($oSrvAcc->getUserByName($sRecipient), $oCurrUser))
                	{
                	    $this->addErrors(App::format('Mod_messenger.user.block', array($sRecipient)));
					}
                	if(empty($aPost['text']))
                	{
                	    $this->addErrors(App::format('Mod_messenger.text.error'));
					}

                	$aErrors = $this->getErrors();

                	if(empty($aErrors))
                	{
                		$oSrvAccA = &App::getModuleService('Account', 'Account');
						/* @var $oSrvSec PhpFox_Mod_Account_ServiceAccount */

						$oUser = &$oSrvAccA->getByUsername($aPost['to']);

						$aPost['to'] = $oUser->aData['user'];
						
						$aPost['text'] = $oTxt->parseText($aPost['text'], 0, 0, 1);
						
	                	if($oSrvMessenger->sendMessage($aPost, $oSrvAcc->getCurrentUserLogin()))
	                	{
	                	   	$_SESSION['sSend'] = $aPost['to'];
	                	    App::gotoUrl();
	                	}
	                	else
	                	{
	               		   $this->addErrors($oSrvMessenger->getErrors());
						}
	                }
				}
				$aMess = $aPost;
                break;
            case 'sPrev':
                $aPreview = $aPost;
				$aMess = $aPost;
                break;
        }

        if (!empty($_SESSION['compose']))
        {
            $bSingle = $sRecipient ? true : false;

            $aMess['title'] = $_SESSION['compose']['subject'];
            $aMess['text'] = $_SESSION['compose']['message'];

            unset($_SESSION['compose']);
        }


        if(!empty($_SESSION['sSend']))
        {
            $sSend = $_SESSION['sSend'];
            unset($_SESSION['sSend']);
        }
        else
            $sSend = false;
        if (!isset($aMess['title']))
        {
            $aMess['title'] = '';
        }
        $aMess['title'] = htmlspecialchars($aMess['title']);
        $oTpl = &$this->_oModule->getTemplate();

		$oTpl->assign(array(
							 'sSend'		=> $sSend,
							 'sBoxName'		=> $sBoxName,
							 'bSingle'		=> $bSingle,
							 'bQuot'        => isset($bQuot) ? $bQuot : false,
							 'sRecipient'	=> !empty($sRecipient) ? $sRecipient : '',
							 'sImageUrl'    => App::getParam('user.pic.url'),
        			    ));
	    $oTpl->assignSrc(array(
			'jsValidation' => $oValidator->makeJS(),
		    'jsValMess'    => $oValidator->makeJsMess(),
		    'aErrors'	   => $this->getErrors(),
		    'aMailInfo'    => !empty($aMalInfo) ? $aMalInfo : false,
		    'aMess'		   => !empty($aMess) ?    $aMess : '',
		    'aPrev'		   => !empty($aPreview) ? $aPreview : false,
		));

        $sHtml = $oTpl->fetch('Compose.html');

        return $sHtml;
    }
}
?>